<template>
  <div v-loading="isLoad" class="video-container pxy20">
    <h2>上传视频课程</h2>
    <ElForm ref="icRefs" :model="curObj" class="myFrom" :rules="videoRules" label-width="140px" label-position="left">
      <ElFormItem label="课程名称" prop="title">
        <ElInput v-model="curObj.title" placeholder="课程名称" style="width: 400px" />
      </ElFormItem>
      <ElFormItem label="课程概述" prop="summary">
        <ElInput
          v-model="curObj.summary"
          placeholder="课程概述"
          style="width: 400px"
          type="textarea"
          :rows="6"
          maxlength="150"
          show-word-limit
        />
      </ElFormItem>
      <ElFormItem label="课程分类" prop="type">
        <el-select v-model="curObj.type" placeholder="请选择课程分类">
          <el-option label="留学前课程" value="1" />
          <el-option label="留学后课程" value="2" />
        </el-select>
      </ElFormItem>
      <ElFormItem label="学生背景" prop="studentBackground">
        <ElInput v-model="curObj.studentBackground" placeholder="学生背景" style="width: 400px" type="textarea" :rows="3" />
      </ElFormItem>
      <ElFormItem label="封面" prop="cover">
        <el-upload
          class="avatar-uploader"
          action="https://api.ukshuxi.com/api/file/uploadFile"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :on-error="uploadError"
          :on-success="uploadCoverSuccess"
        >
          <img v-if="curObj.cover" :src="baseUrl + curObj.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </ElFormItem>

      <ElFormItem label="上传视频" prop="video">
        <el-upload
          class="avatar-uploader"
          action="https://api.ukshuxi.com/api/file/uploadFile"
          :show-file-list="false"
          :on-success="uploadVideoSuccess"
          :before-upload="beforeUpload"
          :on-error="uploadError"
        >
          <video v-if="curObj.video" :src="baseUrl + curObj.video" class="video" controls />
          <el-button v-else><i class="el-icon-upload2" /> 上传文件</el-button>
        </el-upload>
      </ElFormItem>
      <ElFormItem label="日期" prop="articleDate">
        <el-date-picker v-model="createDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" />
        <el-time-picker v-model="createTime" placeholder="选择时间" format="HH:mm:ss" />
      </ElFormItem>
      <ElFormItem label="是否设置为推荐位" label-width="160px">
        <el-radio v-model="curObj.isRecommended" :label="true">是</el-radio>
        <el-radio v-model="curObj.isRecommended" :label="false">否</el-radio>
      </ElFormItem>
      <ElFormItem label="是否设置为Banner位" label-width="160px">
        <el-radio v-model="curObj.isBanner" :label="true">是</el-radio>
        <el-radio v-model="curObj.isBanner" :label="false">否</el-radio>
      </ElFormItem>
    </ElForm>
    <br>
    <ElButton type="primary" @click="submitUpload">发布</ElButton>
  </div>
</template>

<script>
import { videoRules } from '@/utils/formVerify'
import { VIDEO_EDIT, VIDEO_ADD } from '@/api/manage/video'

export default {
  name: 'VideoEdit',
  data() {
    return {
      curObj: {
        title: '',
        summary: '',
        studentBackground: '',
        video: '',
        isRecommended: false,
        cover: '',
        isBanner: false,
        courseCreateTime: '',
        type: '' // 分类：1留学前，2留学后
      },
      createTime: '',
      createDate: '',
      baseUrl: 'https://api.ukshuxi.com',
      videoRules,
      fileList: [],
      isLoad: false
    }
  },
  mounted() {
    const row = this.$route.params
    if (Object.keys(row).length !== 0) {
      this.curObj = row
      this.createDate = this.curObj.courseCreateTime.slice(0, 11)
      this.createTime = new Date(this.curObj.courseCreateTime)
    }
  },
  methods: {
    submitUpload() {
      if (!this.createDate || !this.createTime) {
        this.$message.warning('请选择日期和时间')
      }
      this.$refs['icRefs'].validate((i) => {
        this.curObj.courseCreateTime = this.createDate + ' ' + this.createTime.getHours() + ':' + this.createTime.getMinutes()
        if (i) {
          this.curObj.id
            ? VIDEO_EDIT(this.curObj).then((res) => {
              this.apiRes(res)
            })
            : VIDEO_ADD(this.curObj).then((res) => {
              this.apiRes(res)
            })
        }
      })
    },
    uploadCoverSuccess(res) {
      this.curObj.cover =
        '/drdImg/' + res.data.fileName
      this.$message({
        message:
          +res.code === 200 ? '封面上传成功!' : '封面上传失败，请联系系统管理员!',
        type: +res.code === 200 ? 'success' : 'error'
      })
      this.isLoad = false
    },
    beforeUpload() {
      this.isLoad = true
    },
    uploadError(err) {
      this.isLoad = false
      console.log('err', err)
      this.$message({
        showClose: true,
        message: '上传文件失败，请重试或联系管理员',
        type: 'error'
      })
    },
    uploadVideoSuccess(res) {
      this.curObj.video =
        '/drdImg/' + res.data.fileName
      this.$message({
        message:
          +res.code === 200 ? '视频上传成功!' : '视频上传失败，请联系系统管理员!',
        type: +res.code === 200 ? 'success' : 'error'
      })
      this.isLoad = false
    },
    apiRes(res) {
      const i = +res['result'] === 0

      this.$message({
        message: i
          ? this.curObj.id
            ? '视频更新成功!'
            : '视频增加成功！'
          : '视频编辑失败!' + res.msg,
        type: i ? 'success' : 'error'
      })
      i ? this.$refs['icRefs'].resetFields() : ''
      i && this.$router.push({ name: 'videoIndex' })
    }
  }
}
</script>

<style lang="scss" scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-date-editor {
  margin-right: 10px;
}
  .video {
    width: 320px;
    height: 180px;
  }
</style>
